/*
   Copyright 2022 cmanlh

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/**
 * date display
 */
import JscConfig from './config.js';
import JscDatabind from './databind.js';
export default class JscDisplaySelect extends JscDatabind {
    static get observedAttributes() { return ['value', 'jscbind', 'options'] }

    #value;
    #jscBind = null;
    #span = null;
    #options = new Map();

    constructor() {
        super();
        this.#init();
        this.#render();
    }

    connectedCallback() {
    }

    disconnectedCallback() {
    }

    attributeChangedCallback(name, oldValue, newValue) {
        switch (name) {
            case 'value': {
                if (oldValue != newValue) {
                    this.value = newValue;
                }
                break;
            }
            case 'jscbind': {
                if (oldValue != newValue) {
                    this.#jscBind = newValue;
                }
                break;
            }
        }
    }

    bindRefresh(bindStamp) {
        if (super.bindRefresh(bindStamp)) {
            if (null != this.bind && null != this.#jscBind) {
                let _newValue = this.fetchBindValue(this.#jscBind);
                if (null == _newValue) {
                    this.#span.textContent = null;
                    self.#span.title = self.#span.textContent;
                } else {
                    this.#value = _newValue;
                    let texts = '';
                    this.#value.split(',').forEach(val=>{
                        texts = texts.concat('，').concat(self.#options.get(val));
                    })
                    self.#span.textContent = texts.substring(1);
                    self.#span.title = self.#span.textContent;
                }
            }
        }
    }

    set value(val) {
        let self = this;
        let oldValue = self.#value;
        let newValue = val;
        if (oldValue == newValue) {
            return;
        }
        self.#value = newValue;
        if (null == self.#value) {
            self.#span.textContent = null;
            self.#span.title = self.#span.textContent;
        } else {
            let texts = '';
            this.#value.split(',').forEach(val=>{
                texts = texts.concat('，').concat(self.#options.get(val));
            })
            self.#span.textContent = texts.substring(1);
            self.#span.title = self.#span.textContent;
        }
        this.updateBindValue(this.#jscBind, this.#value);
    }

    get value() {
        return this.#value;
    }

    #init() {
        this.#jscBind = this.getAttribute('jscbind');
        let _options = this.getAttribute('options');
        if (_options) {
            let self = this;
            let options = JSON.parse(_options);
            options.forEach((option) => {
                self.#options.set(option.value + "", option.label);
            });
        }
    }

    #render() {
        const shadowRoot = this.attachShadow({ mode: 'open' });
        shadowRoot.innerHTML = `
        <link rel='stylesheet' type='text/css' href='${JscConfig.cssUrl}'/>
        <span class='jscDisplay'></span>
        `;
        this.#span = this.shadowRoot.querySelector('span');
    }
}

if (!customElements.get('jsc-display-select')) {
    window.customElements.define("jsc-display-select", JscDisplaySelect);
}